package com.frxuexi.department

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Icon
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.department.entity.DepartmentEntity
import com.frxuexi.department.intent.DepartIntent
import com.frxuexi.department.viewmodel.DepartViewModel
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.NavPath
import com.frxuexi.lib_base.RequestType
import com.frxuexi.lib_base.UiState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Date

@Composable
fun DepartmentPage(
    navHostController: NavHostController,
    vm: DepartViewModel = hiltViewModel(),
    scope: CoroutineScope = rememberCoroutineScope()
) {

    var list = remember {
        SnapshotStateList<DepartmentEntity>()
    }

    LaunchedEffect(key1 = "", block = {
        scope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess -> {
                        if (it.type == RequestType.DEFAULT) {
                            list.clear()
                            list.addAll(it.data as Collection<DepartmentEntity>)
                        } else if (it.type == RequestType.DELETE) {//删除成功
                            //重新请求列表
                            vm.sendIntent(DepartIntent.getDeparts)
                        }

                    }

                    is UiState.onError -> {
                        Log.i("===", it.msg)
                    }

                    else -> {}
                }
            }
        }

        vm.sendIntent(DepartIntent.getDeparts)
    })
    Scaffold(floatingActionButton = {
        Icon(
            painter = painterResource(id = R.drawable.icon159),
            contentDescription = "",
            Modifier.clickable { navHostController.navigate(NavPath.LOCALDOC) })
    }) {
        Column(Modifier.padding(it)) {
            /*Box(
                Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
            ) {
                Icon(imageVector = Icons.Default.ArrowBack,
                    contentDescription = "",
                    modifier = Modifier.clickable {
                        navHostController.popBackStack()
                    })
                Text(text = "文件", Modifier.fillMaxWidth(), textAlign = TextAlign.Center)
            }*/
            BaseTitle(navHostController = navHostController, title = "文件")


            LazyColumn(content = {
                items(list) {
                    DepartItem(entity = it) {
//                    list.remove(it)
                        vm.sendIntent(DepartIntent.deleteDoc(it.fileId))
                    }
                }
            })
        }
    }


}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun DepartItem(entity: DepartmentEntity, onClick: (DepartmentEntity) -> Unit) {
    //删除提示框是否显示
    var isShow by remember {
        mutableStateOf(false)
    }
    var name by remember {
        mutableStateOf("")
    }

    Row(
        Modifier
            .padding(start = 20.dp, top = 10.dp, end = 20.dp)
            .combinedClickable(onLongClick = {
                isShow = true
            }, onClick = {
                ToastUtils.showLong("点击")
            })

        /* .clickable { isShow = true }*/
    ) {

        AsyncImage(
            model = entity.filePath,
            contentDescription = "",
            modifier = Modifier
                .size(60.dp)
                .clip(RoundedCornerShape(20.dp)),
            contentScale = ContentScale.Crop,
        )
        Spacer(modifier = Modifier.width(10.dp))
        Column(Modifier.height(60.dp), verticalArrangement = Arrangement.SpaceAround) {
            name = entity.fileName.split(Regex("[.]"))[0]
            Text(text = name)
            val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val time = format.format(Date(entity.fileTime))
            Text(text = time)
        }
    }

    if (isShow) {
        AlertDialog(onDismissRequest = { isShow = false }, confirmButton = {
            TextButton(onClick = {
                onClick(entity)
                isShow = false
            }) {
                Text(text = "确定")
            }
        }, dismissButton = {
            TextButton(onClick = { isShow = false }) {
                Text(text = "取消")
            }
        },
            title = { Text(text = "删除") }, text = { Text(text = "确认删除$name 吗？") })
    }
}